#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Generate Image Maps                                                #
#                                                                           #
# 2dx.org, GNU Plublic License.                                             #
#                                                                           #
# Created..........: 02/20/2006                                             #
# Last Modification: 09/20/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 97
#
# MANUAL: This script makes use of MRC and CCP4 commands to generate the projection maps of all selected images, using the latest phase origin settings.
#
# PUBLICATION: 3D reconstruction of two-dimensional crystals: <A HREF="http://www.ncbi.nlm.nih.gov/pubmed/26093179">Arch Biochem Biophys 581, 68-77 (2015)</A>
# PUBLICATION: 3D Reconstruction from 2D crystal image and diffraction data: <A HREF="http://dx.doi.org/10.1016/S0076-6879(10)82004-X">Methods Enzymol. 482, Chapter 4, 101-129 (2010)</A>
# PUBLICATION: 2dx - Automated 3D structure reconstruction from 2D crystal data: <A HREF="http://journals.cambridge.org/action/displayAbstract?aid=1943200">Microscopy and Microanalysis 14(Suppl. 2), 1290-1291 (2008)</A>
# PUBLICATION: 2dx_merge - Data management and merging for 2D crystal images: <A HREF="http://dx.doi.org/10.1016/j.jsb.2007.09.011">J. Struct. Biol. 160(3), 375-384 (2007)</A>
#
# DISPLAY: RESMIN
# DISPLAY: RESMAX
# DISPLAY: SYM
# DISPLAY: ALAT
# DISPLAY: tempfac
# DISPLAY: MergeIQMAX
# DISPLAY: realang
# DISPLAY: realcell
# DISPLAY: make_reference
# DISPLAY: merge_reference
# DISPLAY: merge_ref_num
# DISPLAY: create_PS
# DISPLAY: merge_data_type
# DISPLAY: merge_res_limit
# DISPLAY: Thread_Number
# DISPLAY: npo_line1
# DISPLAY: npo_line2
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set tempkeep = ""
set RESMIN = ""
set RESMAX = ""
set ALAT = ""
set realang = ""
set realcell = ""
set MergeIQMAX = ""
set tempfac = ""
set SYM = ""
set make_reference = ""
set merge_ref_num = ""
set realang = ""
set zstarwin = ""
set ctfrev = ""
set MergeHKMAX = ""
set merge_res_limit = ""
set create_PS = ""
set merge_data_type = ""
set merge_reference = ""
set RFACAMP = ""
set Thread_Number = ""
set npo_line1 = ""
set npo_line2 = ""
set zstarrange = ""
#
#$end_vars
#
echo "<<@progress: 1>>"
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
#
set split = ($realcell:as/,/ /)
set cellx = $split[1]
set celly = $split[2]
#
echo tempkeep = ${tempkeep}
echo RESMIN = ${RESMIN}
echo RESMAX = ${RESMAX}
echo ALAT = ${ALAT}
echo realang = ${realang}
echo realcell = ${realcell}
echo tempfac = ${tempfac}
echo MergeHKMAX = ${MergeHKMAX}
echo SYM = ${SYM}
#
set imagenumber = 1001
set imagename = "merge"
#
set scriptname = 2dx_generateImageMaps
set merge_modus="3D"
#
source ${proc_2dx}/2dx_merge_makedirs 
#
\rm -f LOGS/${scriptname}.results
#
set RESULTSPS = "n"
set RESULTSMRC = "n"
\rm -rf RESULTS-MRC
\rm -rf RESULTS-TIFF
\rm -rf RESULTS-PS
\mkdir RESULTS-MRC
\mkdir RESULTS-TIFF
echo 1000001 > RESULTS-TIFF/filenum.tmp
\mkdir RESULTS-PS
#
set date = `date`
echo date = ${date}
#
set olddir = $PWD
cd ..
set basedir = $PWD
cd ${olddir}
#
if ( ${ALAT} == "0" || ${ALAT} == "0.0" ) then
  ${proc_2dx}/protest "ALAT is not defined."
endif
set ALATnew = `echo ${ALAT} | awk '{ if ( $1 < 0 ) { s = -$1 } else { s = $1 }} END { print s }'`
if ( ${ALAT} != ${ALATnew} ) then
  set ALAT = ${ALATnew}
  echo "set ALAT = ${ALAT}" >> LOGS/${scriptname}.results
endif
#
set zwin = `echo ${ALAT} | awk '{ s = 1.0 / ( 2.0 * $1 ) } END { print s }'`
echo zwin = $zwin
#
set ILIST_VAL = 0
#
#???? Check this ???
set SCL = 1
echo "SCL = ${SCL}"
#
# contrast for grey plot
set scale = 1
echo "scale = ${scale}"
#
# The following is to make sure that for the next "Import Images", the default is correctly initialized.
set initialization_reset = "y"
set initialization_executable = "y"
echo "set initialization_reset = ${initialization_reset}" >> LOGS/${scriptname}.results
echo "set initialization_executable = ${initialization_executable}" >> LOGS/${scriptname}.results
#
#############################################################################
${proc_2dx}/linblock "sourcing sym2spsgrp_sub.com"
#############################################################################
#
source ${proc_2dx}/2dx_sym2spcgrp_sub.com
#
echo SYM = ${SYM}
echo spcgrp = ${spcgrp}
echo CCP4_SYM = ${CCP4_SYM}
#
############################################################################# 
${proc_2dx}/lin "2dx_merge_makedirs - to create all required subdirectories"
#############################################################################
#
${proc_2dx}/2dx_merge_makedirs
#
echo "<<@progress: 10>"
#
if ( ${make_reference} == "y" ) then
  #
  set NPRG = 1
  if ( ${merge_reference} == '0' ) then
    # Use merge.aph files
    if ( ! -e APH/merge.aph ) then
      ${proc_2dx}/linblock "ERROR: APH/merge.aph not found. No reference for refinement available."
      ${proc_2dx}/protest "Run Merging script first."
    endif
    set reference_file="APH/merge.aph"
  endif 
  if ( ${merge_reference} == '1' ) then
    # Use interpolated lattice lines merge3Dref_MRClefthanded.mtz
    if ( ! -e merge3Dref_MRClefthanded.mtz ) then
      ${proc_2dx}/linblock "ERROR: merge3Dref_MRClefthanded.mtz not found. No reference for refinement available."
      ${proc_2dx}/protest "Run Merging script first."
    endif
    set reference_file="merge3Dref_MRClefthanded.mtz"
    set NPRG = 3
  endif
  #
  if ( ${merge_ref_num} != "0" ) then
    if ( -d REGISTERS/Reg_${merge_ref_num} ) then
      #############################################################################
      ${proc_2dx}/linblock "Using merged dataset from register ${merge_ref_num}."
      #############################################################################  
      echo "::" `cat REGISTERS/Reg_${merge_ref_num}/COMMENT.txt | head -1`
      echo "::" `cat REGISTERS/Reg_${merge_ref_num}/COMMENT.txt | head -2 | tail -1`
      echo " "
      if ( ${NPRG} == "3" ) then
        # Use MTZ file
        if ( ! -e REGISTERS/Reg_${merge_ref_num}/merge3D_MRClefthanded.mtz ) then
          ${proc_2dx}/protest "ERROR: merge3D_MRClefthanded.mtz not existing in register ${merge_ref_num}."
        else
          echo ":Copying REGISTERS/Reg_${merge_ref_num}/merge3D_MRClefthanded.mtz ."
          \cp -f REGISTERS/Reg_${merge_ref_num}/merge3D_MRClefthanded.mtz .
        endif
        #
        if ( ! -e REGISTERS/Reg_${merge_ref_num}/merge3Dref_MRClefthanded.mtz ) then
          ${proc_2dx}/protest "ERROR: merge3Dref_MRClefthanded.mtz not existing in register ${merge_ref_num}."
        else
          echo ":Copying REGISTERS/Reg_${merge_ref_num}/merge3Dref_MRClefthanded.mtz ."
          \cp -f REGISTERS/Reg_${merge_ref_num}/merge3Dref_MRClefthanded.mtz .
        endif
      else
        # Use APH file
        if ( ! -e REGISTERS/Reg_${merge_ref_num}/merge2D_MRClefthanded.mtz ) then
          ${proc_2dx}/protest "ERROR: merge2D_MRClefthanded.mtz not existing in register ${merge_ref_num}."
        else
          echo ":Copying REGISTERS/Reg_${merge_ref_num}/merge2D_MRClefthanded.mtz ." 
          \cp -f REGISTERS/Reg_${merge_ref_num}/merge2D_MRClefthanded.mtz .
        endif
        if ( ! -e REGISTERS/Reg_${merge_ref_num}/merge.aph ) then
          ${proc_2dx}/protest "ERROR: merge.aph not existing in register ${merge_ref_num}."
        else
          echo ":Copying REGISTERS/Reg_${merge_ref_num}/merge.aph APH/merge.aph"
          \cp -f REGISTERS/Reg_${merge_ref_num}/merge.aph APH/merge.aph
        endif
      endif
    else
      ${proc_2dx}/protest "ERROR: Register ${merge_ref_num} does not contain data."
    endif
    #
  endif
  #
  #############################################################################
  ${proc_2dx}/linblock "Compile origtilt script for reference projections"
  #############################################################################
  #
  set scriptBfile = "2dx_merge_scriptB.com"
  set postprocessingfile = "2dx_merge_postprocessing.com"
  #
  set genref = "1"
  #
  set dirfile = "2dx_merge_dirfile.dat"
  set number = 1
  set IVERBOSE = 1
  set NBM = F
  set NTL = F
  set ITAXASTEP = 1
  set RTAXASIZE = 0.5
  set ITANGLSTEP = 1
  set RTANGLSIZE = 0.5
  set MergeStepSize = "0.001"
  set IBOXPHS = 3
  set Loc_Thread_Number = 1
  #
  ${bin_2dx}/2dx_merge_compileB.exe << eot
${reference_file}
${scriptname}-tmp.results
${scriptname}-tmp.reflections
${scriptname}-tmp.console
${proc_2dx}
${bin_2dx}
${dirfile}
${scriptBfile}
${postprocessingfile}
${genref}
${spcgrp}
${realcell}
${realang}
${zstarwin}
${ALAT}
${IVERBOSE}
${MergeStepSize}
${RFACAMP}
${IBOXPHS}
${NPRG}
${merge_reference}
${NBM}
${NTL}
${ITAXASTEP}
${RTAXASIZE}
${ITANGLSTEP}
${RTANGLSIZE}
${MergeIQMAX}
${MergeHKMAX}
${ctfrev}
${merge_res_limit}
${RESMIN}
${RESMAX}
${merge_data_type}
${Loc_Thread_Number}
${ILIST_VAL}
0
eot
  #
  echo "<<@progress: 20>>"
  #
  #############################################################################
  ${proc_2dx}/linblock "Launch origtilt script to create reference projections."
  ${proc_2dx}/linblock "CHECK OUTPUT in LOG: origtilt B output"
  #############################################################################
  #
  echo "# IMAGE: SCRATCH/job_01_${scriptBfile} <CSH: refinement script>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/job_01_${postprocessingfile} <CSH: refinement postprocessing script>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/2dx_merge_scriptB.log <LOG: origtilt B output>" >> LOGS/${scriptname}.results
  SCRATCH/job_01_${scriptBfile} > SCRATCH/2dx_merge_scriptB.log
  # cat SCRATCH/2dx_merge_scriptB.out
  #
  #
  #############################################################################
  ${proc_2dx}/linblock "Launch refinement postprocessing script"
  #############################################################################
  #
  source SCRATCH/job_01_${postprocessingfile} >> SCRATCH/2dx_merge_scriptB.log
  #
  echo "################################################"
  echo "################################################"
  echo "output in file SCRATCH/2dx_merge_scriptB.log"
  echo "################################################"
  echo "################################################"
  #
  echo "# IMAGE: SCRATCH/${scriptname}.results <LOG: Refinement results file>" >> LOGS/${scriptname}.results
  cat SCRATCH/job_01_${scriptname}-tmp.console > SCRATCH/${scriptname}.results
  #
  # Don't evaluate refinement results here:
  # cat SCRATCH/job_01_${scriptname}-tmp.results >> LOGS/${scriptname}.results
  \rm -f SCRATCH/job_01_${scriptname}-tmp.results
  #
endif
#
echo "<<@progress: 40>"
#
#############################################################################
${proc_2dx}/linblock "Compile scripts to re-generate maps"
#############################################################################
#
set dirfile = "2dx_merge_dirfile.dat"
set scriptMfile = "2dx_merge_scriptM"
#
echo "dummy" > SCRATCH/job_00_${scriptMfile}.com
\rm -f SCRATCH/job_*_${scriptMfile}.com
#
echo "dummy" > SCRATCH/job_00_${scriptMfile}.log
\rm -f SCRATCH/job_*_${scriptMfile}.log
#
${bin_2dx}/2dx_merge_compileM.exe << eot
${basedir}
${dirfile}
${scriptMfile}
${realcell}
${realang}
${tempfac}
${zstarrange}
${ctfrev}
${npo_line1}
${npo_line2}
${ALAT}
${Thread_Number}
${merge_data_type}
eot
#
#############################################################################
${proc_2dx}/linblock "Launching scripts"
#############################################################################
#
echo "<<@progress: 20>>"
#
set maxthread_gt_9 = `echo ${Thread_Number} | awk '{ if ( $1 > 9 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${maxthread_gt_9} == '1' ) then
  set maxthread_with_zero = ${Thread_Number}
else
  set maxthread_with_zero = "0"${Thread_Number}
endif
if ( ${Thread_Number} == "1" ) then
  echo "# IMAGE: SCRATCH/job_01_${scriptMfile}.com <CSH: Script>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/job_01_${scriptMfile}.com.log <LOG: Script output>" >> LOGS/${scriptname}.results
else
  echo "# IMAGE: SCRATCH/job_01_${scriptMfile}.com <CSH: First (01) script>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/job_01_${scriptMfile}.com.log <LOG: First (01) script output>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/job_${maxthread_with_zero}_${scriptMfile}.com <CSH: Last (${maxthread_with_zero}) script>" >> LOGS/${scriptname}.results
  echo "# IMAGE: SCRATCH/job_${maxthread_with_zero}_2dx_${scriptMfile}.com.log <LOG: Last (${maxthread_with_zero}) script output>" >> LOGS/${scriptname}.results
endif
#
foreach scriptM ( SCRATCH/job_*_${scriptMfile}.com )
  chmod +x ${scriptM}
  if ( ${scriptM} != SCRATCH/job_${maxthread_with_zero}_${scriptMfile}.com ) then
    echo Background nohup ${scriptM} \> ${scriptM}.log \&
    nohup ${scriptM} ${proc_2dx} ${bin_2dx} ${dir_ccp4} ${MergeIQMAX} ${RFACAMP} ${RESMIN} ${RESMAX} ${CCP4_SYM} ${zwin} ${make_reference} ${create_PS} ${scriptname} > ${scriptM}.log &
  else
    echo Forground ${scriptM} \> ${scriptM}.log
    ${scriptM} ${proc_2dx} ${bin_2dx} ${dir_ccp4} ${MergeIQMAX} ${RFACAMP} ${RESMIN} ${RESMAX} ${CCP4_SYM} ${zwin} ${make_reference} ${create_PS} ${scriptname} > ${scriptM}.log
  endif
end
#
#
echo "################################################"
echo "################################################"
echo "output in file SCRATCH/job_XX_${scriptMfile}.com.log"
echo "################################################"
echo "################################################"
#
echo "Refinement jobs produced the following output files:"
touch SCRATCH/job_01_${scriptMfile}.com
\ls -l SCRATCH/job_*_${scriptMfile}.com
touch SCRATCH/job_01_${scriptMfile}.com.log
\ls -l SCRATCH/job_*_${scriptMfile}.com.log
#
sleep 1
#
\rm -f SCRATCH/${scriptMfile}.console
echo "# IMAGE: SCRATCH/${scriptMfile}.console <LOG: Console output from merging>" >> LOGS/${scriptname}.results
foreach scriptMconsole ( SCRATCH/job_*_${scriptMfile}.com.log ) 
  cat ${scriptMconsole} >> SCRATCH/${scriptMfile}.console
  \rm -f ${scriptMconsole}
end
#
echo "<<@progress: 100>>"
#
#
#############################################################################
#                                                                           #
echo "::"
${proc_2dx}/linblock "Done."
#                                                                           #
#############################################################################
#
exit
#
# Only to display this script in 2dx_merge:
source ${proc_2dx}/2dx_merge_redoMap_sub.com
#
